home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Does C convert float to double internally ?
- Date: Thu, 04 Apr 96 17:57:17 GMT
- Organization: none
- Message-ID: <828640637snz@genesis.demon.co.uk>
- References: <4jsllh$hkf@harbinger.cc.monash.edu.au> <4jv2ho$r1o@harbinger.cc.monash.edu.au>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jv2ho$r1o@harbinger.cc.monash.edu.au>
- bcheung@yoyo.cc.monash.edu.au "Biggles Cheung" writes:
-
- >Biggles Cheung (bcheung@yoyo.cc.monash.edu.au) wrote:
- >: I was told that C compiler automatically converts "float" variable
- >: internally to "double" for + - * / , etc operations. Is this true?
- >
- >After I have read some books on C/C++, they all say that K&R C used to
- >do _implicit_ conversion from float to double both in unary and binary
- >operations, while ANSI C has dropped the implicit conversion and happily
- >allows float to be float at all time.
-
- ANSI allows either approach, at least in effect.
-
- 6.1.2.5
-
- "The values of floating point operands and the results of floating point
- expressions may be represented in greater precision and range than that
- required by the type; the types are not changed thereby"
-
- In fact the calculation can be performed in any representation greater
- than the one in question. In this case, float, it could be double,
- long double, or a representation that doesn't correspond to any C type.
-
- What you are guaranteed is:
-
- 6.2.1.4
-
- "When a float is promoted to double or long double, or a double is promoted
- to long double, its value is unchanged"
-
- i.e. the conversion doesn't introduce inaccuracies. I guess that doesn't
- necessarily apply in this case since strictly no type conversion is happening,
- just a change of representation.
-
- Assignment and casting are guaranteed to convert to the correct representation
- for the target type (or at least act as if they have).
-
- >Can I safely assume now that ALL current C/C++ compliers are following ANSI C
- >standard?
- >
- >:
- >: Can I force the C compiler not to do the conversion as I have an
- >: assignment on finding out various computing errors of different
- >: precisions?
- >:
- >
- >I have read also the Turbo C++ manual and it says "tcc -ff- " command
- >line option can force the command line compiler to follow STRICT ANSI C
- >rule on conversion, i.e. no implicit conversion.
- >
- >Can I now be sure that if I use "tcc -ff- program.c", there is _no_
- >implicit conversion? But what about the IDE interface?
-
- Often the floating point registers on a system hold more precision for
- accuracy considerations. You probably can't stop them using it. However the
- result must still be a float as far as the type system is concerned.
-
- >Can someone suggest a simple program to test if there is any implicit
- >conversion happening behind the curtain?
-
- #include <stdio.h>
-
- ...
-
- float a, b;
-
- printf("%lu\n", (unsigned long)sizeof(a+b));
-
- If float and double have different representations on your system then
- the chances are that they will have different sizes. For a conforming
- implementation this must print out the same value as sizeof(float). There's
- nop guarantee that the expression a+b won't use more bits of precision
- internally though.
-
- >If I am to find the single precision value of say, 12.3457 to the power
- >of 13 by multiplying 12.3456 thirteen times, does that mean I have to
- >store the result of every _single_ multiplication to a temporary variable
- >of float to enforce the single precision?
-
- You could cast the result of each operator in the expression. This is an
- area where I wouldn't trust the compiler much though
-
- >What if I have a very complicated mathematical expression like Taylor's
- >series plus sqrt plus more ... ? How many temporary variables I need and
- >when is the best time or where is the best spot to insert such
- >_nuisance_?
-
- Typically the extra precision is an advantage, not a disadvantage.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-